<!-- pass a variable to included page -->
<%@ page import="bcared.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%
	String today = null;
	Today t = new Today();
	today = t.getToday();
	request.setAttribute("picture", "Customer");
	request.setAttribute("title", "Dispatch Home");
	request.setAttribute("header", "Today is "
			+ today);

	ResultSet rs = null;
	CallableStatement csmt = null;
	PreparedStatement ps = null;
	OraDB myDB = new OraDB();

	myDB.createConnection();
	String myQuery = "select SOID from serviceOrders where sostatusid NOT IN (1, 7, 8, 9) ORDER BY openDate ASC";
	ps = myDB.conn.prepareStatement(myQuery);
	rs = ps.executeQuery();

	ArrayList<ServiceOrder> serviceOrdersArrayList = new ArrayList<ServiceOrder>();
	ArrayList<Engineer> availableEngineers = new ArrayList<Engineer>();
	int SOID = 0;

	while (rs.next()) {
		serviceOrdersArrayList.add(new ServiceOrder(rs.getInt("SOID")));
	}

	myDB.closeConnection();

	if (request.getParameter("SOID") != null) {
		SOID = Integer.parseInt(request.getParameter("SOID"));
		
		
		ServiceOrder so = new ServiceOrder(SOID);

		myDB.createConnection();
		String myQuery2 = "select distinct Engineers.engineerID from engineers"
				+ " INNER JOIN EngineersModalities ON EngineersModalities.engineerID = ENGINEERS.ENGINEERID"
				+ " WHERE modalityID = ? and Engineers.SOID IS NULL";
				
		ps = myDB.conn.prepareStatement(myQuery2);
		ps.setInt(1, so.getModalityID());
		rs = ps.executeQuery();
		
		while (rs.next()) {
			availableEngineers.add(new Engineer(rs.getInt("engineerID")));
		}
	}
	//Integer.parseInt(request.getParameter("customer
%>
 <%@ include file="header.jsp"%>
 
 <TD width="850" vAlign="top">
 	<form name="dispatch" method="post" action="dispatchassign.jsp">
 	<table>
 		<tr>
 			<td><h2>Pending Service Orders</h2></td>
 			<td><h2>Available Certified Engineers</h2></td>
 		</tr>
 		<tr>
 			<td>
 				SOID, Cust Name, Serialnumber<br/>
 				<select name="SOID" multiple="multiple" size="20" onClick="return updateEngineerList()">
						<%
							for (int i = 0; i < serviceOrdersArrayList.size(); i++) {
						%>
								<option value="<%=serviceOrdersArrayList.get(i).getSOID()%>"><%=serviceOrdersArrayList.get(i).getSOID() + " - " + serviceOrdersArrayList.get(i).getCustomerName() + " - " + serviceOrdersArrayList.get(i).getSerialNumber()%></option>
							<%
								}

							%>
				</select>
 			
 			</td>
 			<td>
 				EngineerID, Name<br/>
 				<select name="engineerID" multiple="multiple" size="20">
 				<%
 				if (request.getParameter("SOID") != null) {
 					for (int i = 0; i < availableEngineers.size(); i++) {
 					%>
 						<option value="<%=availableEngineers.get(i).getEngineerID() %>"><%=availableEngineers.get(i).getEngineerID() + " - " + availableEngineers.get(i).getFirstName() + " " + availableEngineers.get(i).getLastName() + " - " + availableEngineers.get(i).getCellNumber() %></option>
 					<%
 					}
 				}
 				%>
 				</select>
 			</td>
 		</tr>
 	</table>
 	<% 
 	if (request.getParameter("SOID") != null) {
 	%>
 	<input type="submit" name="submit" value="Assign" />
 	<%
 	}
 	%>
 	
 	<input type="hidden" name="selectedSOID" value="<%=SOID %>" />

 	</form>
 
 </td>
</tr>
</table>
<body><br><br></body>

<script Language=JavaScript>
	function updateEngineerList() {
		window.location = "dispatchhome.jsp?SOID=" + dispatch.SOID.value;
		return false;
	}
</script>

</html>